package core.rdd.transform;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;

public class Spark10_KV2 {
    public static void main(String[] args) {
        /**
         *flatMap 是将多集合转换成一个
         */
        // 配置SparkConf指向你的Spark master URL
        SparkConf conf = new SparkConf()
                .setAppName("Spark10_KV2") // 应用名称
                .setMaster("local[*]"); // 替换成你的master地址
        JavaSparkContext sc = new JavaSparkContext(conf);
        // 创建JavaSparkContext，它是与集群交互的主要入口点
        try {
            List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6);

//            JavaRDD<Tuple2<String, Integer>> parallelize = sc.parallelize(tuple2s, 2);
//
//            parallelize.map(new Function<Tuple2<String, Integer>, Object>() {
//                @Override
//                public Object call(Tuple2<String, Integer> v1) throws Exception {
//                    return v1._2*2;
//                }
//            }).collect().forEach(System.out::println);
//

            JavaRDD<Integer> parallelize = sc.parallelize(integers, 2);
            JavaPairRDD<Integer, Integer> objectObjectJavaPairRDD = parallelize.mapToPair(new PairFunction<Integer, Integer, Integer>() {
                @Override
                public Tuple2<Integer, Integer> call(Integer integer) throws Exception {
                    return new Tuple2<Integer,Integer>(integer*2,integer);
                }
            }).mapValues(new Function<Integer, Integer>() {
                @Override
                public Integer call(Integer v1) throws Exception {
                    return v1*10;
                }
            });


            objectObjectJavaPairRDD.collect().forEach(System.out::println);

        } finally {
            sc.close();
        }
    }
}
